package com.audiocn.widget;

import com.audiocn.Utils.Log;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class PostersGallery extends Gallery {

	public static final int PLACE_LEFT_TOP = 0;
	public static final int PLACE_RIGHT_TOP = 1;
	public static final int PLACE_LEFT_BOTTOM = 2;
	public static final int PLACE_RIGHT_BOTTOM = 3;
	
	public interface OnPostersGalleryLitener {
		public void OnClickPlace(int place);
	}
	
	OnPostersGalleryLitener listener;
	int gWidth;
	int gHight;
	float downX, downY, start, end;
	int i;
	
	public PostersGallery(Context context, AttributeSet attrs, OnPostersGalleryLitener listener) {
		super(context, attrs);
		this.listener = listener;
	}
	public PostersGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.listener = null;
	}
	public void setPosterListener(OnPostersGalleryLitener listener) {
		this.listener = listener;
	}
	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > e1.getX();
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int kEvent;
		if (isScrollingLeft(e1, e2)) {
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else {
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		onKeyDown(kEvent, null);
		return true;
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (listener == null) {
			return super.onTouchEvent(event);
		}
		gWidth = this.getWidth() / 2;
		gHight = this.getHeight() / 2;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			downX = event.getX();
			downY = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			if (i == 0) {
				start = event.getRawX();
			} else {
				end = event.getRawX();
			}
			i ++;
			break;
		case MotionEvent.ACTION_UP:
			Log.e("onTouch", "start:(" + start + ") end:(" + end + ") ---:(" + (Math.abs(Math.abs(start) - Math.abs(end))) + ")");
			if ((Math.abs(Math.abs(start) - Math.abs(end)) < 5)) {
				if (gHight > 280) {
					if (downX < gWidth) {
						if (downY < gHight && downY > 60) {
							listener.OnClickPlace(PLACE_LEFT_TOP);
						} else if(downY > gHight && downY < ((gHight * 2) - 60)) {
							listener.OnClickPlace(PLACE_LEFT_BOTTOM);
						}
					} else {
						if (downY < gHight && downY > 60) {
							listener.OnClickPlace(PLACE_RIGHT_TOP);
						} else if(downY > gHight && downY < ((gHight * 2) - 60)) {
							listener.OnClickPlace(PLACE_RIGHT_BOTTOM);
						}
					}
				} else {
					if (downX < gWidth) {
						if (downY < gHight) {
							listener.OnClickPlace(PLACE_LEFT_TOP);
						} else {
							listener.OnClickPlace(PLACE_LEFT_BOTTOM);
						}
					} else {
						if (downY < gHight) {
							listener.OnClickPlace(PLACE_RIGHT_TOP);
						} else {
							listener.OnClickPlace(PLACE_RIGHT_BOTTOM);
						}
					}
				}
			}
			start = 0;
			end = 0;
			i = 0;
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
		
	}
	
}
